part of '../repository.dart';

const _kPendingOrderIdKey = 'id';
const _kPendingOrderUserIdKey = 'user_id';
const _kPendingOrderPurchaseItemSkuKey = 'purchase_item_sku';
const _kPendingOrderYookassaApiPendingPaymentIdKey = 'yookassa_api_pending_payment_id';
const _kPendingOrderTimeKey = 'time';
const _kPendingOrderTestKey = 'test';

class _PendingOrderDto extends Equatable {
  const _PendingOrderDto._internal({
    required this.id,
    required this.userId,
    required this.purchaseItemSku,
    required this.yookassaApiPendingPaymentId,
    required this.orderTimeString,
    required this.test,
  });

  final String id;
  final String userId;
  final String purchaseItemSku;
  final String yookassaApiPendingPaymentId;
  final String orderTimeString;
  final bool test;

  @override
  List<Object?> get props {
    return [
      id,
      userId,
      purchaseItemSku,
      yookassaApiPendingPaymentId,
      orderTimeString,
      test,
    ];
  }

  factory _PendingOrderDto.fromEntity(YookassaPendingOrder pendingOrder) {
    return _PendingOrderDto._internal(
      id: pendingOrder.id,
      userId: pendingOrder.userId,
      purchaseItemSku: pendingOrder.purchaseItemSku,
      yookassaApiPendingPaymentId: pendingOrder.yookassaApiPendingPaymentId,
      orderTimeString: pendingOrder.orderTime.toIso8601String(),
      test: pendingOrder.test,
    );
  }

  factory _PendingOrderDto.fromJson(Map<String, dynamic> json) {
    return _PendingOrderDto._internal(
      id: json[_kPendingOrderIdKey],
      userId: json[_kPendingOrderUserIdKey],
      purchaseItemSku: json[_kPendingOrderPurchaseItemSkuKey],
      yookassaApiPendingPaymentId: json[_kPendingOrderYookassaApiPendingPaymentIdKey],
      orderTimeString: json[_kPendingOrderTimeKey],
      test: json[_kPendingOrderTestKey],
    );
  }

  YookassaPendingOrder toEntity() {
    return YookassaPendingOrder(
      id: id,
      userId: userId,
      purchaseItemSku: purchaseItemSku,
      yookassaApiPendingPaymentId: yookassaApiPendingPaymentId,
      orderTime: DateTime.parse(orderTimeString),
      test: test,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      _kPendingOrderIdKey: id,
      _kPendingOrderUserIdKey: userId,
      _kPendingOrderPurchaseItemSkuKey: purchaseItemSku,
      _kPendingOrderYookassaApiPendingPaymentIdKey: yookassaApiPendingPaymentId,
      _kPendingOrderTimeKey: orderTimeString,
      _kPendingOrderTestKey: test,
    };
  }
}
